/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ejb.common;

import entity.Marchio;
import facade.MarchioFacade;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Valerio
 */
@Stateless
@TransactionAttribute(TransactionAttributeType.SUPPORTS)
public class MarchioManager implements MarchioManagerLocal {

    @PersistenceContext(unitName = "E-Market-ejbPU")
    private EntityManager em;
    @EJB
    private MarchioFacade marchioFacade;

    @Override
    public void creaMarchio(Marchio marchio) {
        Query q = em.createNamedQuery("marchio.cercaPerNome");
        q.setParameter(1, marchio.getNome());
        if (!q.getResultList().isEmpty()) {
            return;
        }
        marchioFacade.create(marchio);
    }

    @Override
    public Marchio cercaPerNome(String nome) {
        Query q = em.createNamedQuery("marchio.cercaPerNome");
        q.setParameter(1, nome);
        List<Marchio> res = q.getResultList();
        if (res.isEmpty()) {
            return null;
        }
        return res.get(0);
    }

    @Override
    public Marchio cercaPerId(Long id) {
        Marchio m = marchioFacade.find(id);
        m.getFoto();
        return m;
    }

    @Override
    public List<Marchio> cercaTutto() {
        List<Marchio> res = marchioFacade.findAll();
        return res;
    }

    @Override
    public void modificaMarchio(Marchio marchio) {
        marchioFacade.edit(marchio);
    }

    @Override
    public void rimuoviMarchio(Marchio marchio) {
        marchioFacade.remove(marchio);
    }
}
